package main

import (
	"encoding/json"
	"fmt"
	"godb/cache"
	"godb/config"
	"sync"

	"github.com/gin-gonic/gin"
)

type Order struct {
	Name string `json:"name"`
	Time int64  `json:"time"`
}

func main() {

	r := gin.Default()

	var order Order
	var mu sync.Mutex //防击穿
	r.GET("/ca1", func(c *gin.Context) {

		key := "keyz"
		val := cache.Hq(key)
		if val != "" {
			json.Unmarshal([]byte(val), &order)
			// fmt.Println("获取缓存", order)
		} else {
			mu.Lock()
			_, err := config.XormDb.Table("user").Where("id = ?", 1).Get(&order)
			if err != nil {
				fmt.Println("数据库user查询失败", err)
				return
			}
			mu.Unlock()
			str, _ := json.Marshal(order)
			cache.Sz(key, string(str), 10)
			// fmt.Println("取数据库", order)
		}
	})

	r.Run()

}